﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace ZK.YCGLRD_WindowsService
{
    public class ServerController : ApiController
    {
       
        /// <summary>
        /// 获取卡号
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string Get()
        {
            return "345";
        }
        [HttpPost]
        public string Post()
        {
            return "456";
        }
        [HttpPost]
        public HttpResponseMessage GetFile()
        {
            /*
             //string filesavename = trainhoursmodel.uploadfile;//保存的文件名
            //string filename = trainhoursmodel.filename;// 原文件名
            //var fileurl = JYZX.Common.UploadFileParam.HoursDeclarationMaterials;//保存地址地址
            //string CurrentPath = System.Web.HttpContext.Current.Server.MapPath(fileurl);//获取物理路径
            //// var filepath = Path.Combine(CurrentPath, filesavename);//文件的完整路径
            ////整合绝对物理路径,获取的地址为manager的系统地址，file 的保存地址需要转web 地址，用replace 替换字符串。
            //// string FilePath = (UploadFileParam.StudyDataPath + DownFile.save_name).Replace("JYZX.ZhuHaiTrain.Web", "JYZX.ZhuHaiTrain.Manager");   //replace(old , new) 
            //string FilePath = Path.Combine(CurrentPath, filesavename); //指出文件的绝对路径
            //return File(new FileStream(FilePath, FileMode.Open), "application/octet-stream", filename);//编码Server.UrlEncode(filename)
           
            return 
            return @"G:\lcw\gitee\hardware_debugging_tools\staticFile\test.txt";//编码Server.UrlEncode(filename)
             */
            try
            {
                var FilePath = @"G:\lcw\gitee\hardware_debugging_tools\staticFile\test.txt";
                var stream = new FileStream(FilePath , FileMode.Open) ;
                HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                response.Content = new StreamContent(stream);
                response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
                {
                    FileName = "test.txt"
                };
                return response;
            }
            catch
            {
                return new HttpResponseMessage(HttpStatusCode.NoContent);
            }
        }

    }
}
